package com.sangcomz.fishbun.ui.picker;

import com.sangcomz.fishbun.BaseAbilitySlice;
import com.sangcomz.fishbun.datasource.PickerIntentDataSourceImpl;
import com.sangcomz.fishbun.permission.PermissionCheck;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventManager;
import ohos.rpc.RemoteException;
import ohos.security.SystemPermission;
import ohos.utils.PacMap;

import static com.sangcomz.fishbun.BaseAbilitySlice.*;
import static com.sangcomz.fishbun.BaseAbilitySlice.GRANT_RESULTS;

public class PickerActivity extends Ability{

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(PickerAbilitySlice.class.getName());
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);

        try {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withAction(PERMISSION_CALLBACK)
                    .build();
            intent.setParam(REQUEST_CODE,requestCode);
            intent.setParam(PERMISSIONS,permissions);
            intent.setParam(GRANT_RESULTS,grantResults);
            intent.setOperation(operation);
            CommonEventData eventData = new CommonEventData(intent);
            CommonEventManager.publishCommonEvent(eventData);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
        try {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withAction(ON_SAVE_ABILITY_STATE)
                    .build();
            intent.setParam(OUT_STATE, outState);
            intent.setOperation(operation);
            CommonEventData eventData = new CommonEventData(intent);
            CommonEventManager.publishCommonEvent(eventData);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        try {
            Intent intent = new Intent();
            Operation operation = new Intent.OperationBuilder()
                    .withAction(ON_RESTORE_ABILITY_STATE)
                    .build();
            intent.setParam(IN_STATE, inState);
            intent.setOperation(operation);
            CommonEventData eventData = new CommonEventData(intent);
            CommonEventManager.publishCommonEvent(eventData);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public static Intent getPickerActivityIntent(Context context,
                                                 long albumId,
                                                 String albumName,
                                                 int albumPosition) {

        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.harmonyos.fishbun")
                .withAbilityName("com.sangcomz.fishbun.ui.picker.PickerActivity")
                .build();
        intent.setOperation(operation);
        intent.setParam(PickerIntentDataSourceImpl.ARG_ALBUM_ID, albumId);
        intent.setParam(PickerIntentDataSourceImpl.ARG_ALBUM_NAME, albumName);
        intent.setParam(PickerIntentDataSourceImpl.ARG_ALBUM_POSITION, albumPosition);
        return intent;
    }
}
